package com.itsu.mongodb.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itsu.interfaces.entity.Comment;
import com.itsu.interfaces.entity.FollowUser;
import com.itsu.interfaces.entity.Video;
import com.itsu.interfaces.service.VideoService;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    MongoTemplate mongoTemplate;


    @Override
    public void saveVideo(Video video) {
        mongoTemplate.save(video);
    }

    @Override
    public List<Video> getVideos() {
        return mongoTemplate.find(new Query(), Video.class);
    }

    @Override
    public List<Video> randomVideo(Integer pageSize) {
        AggregationResults<Video> aggregate = mongoTemplate.aggregate(Aggregation.newAggregation(Video.class, Aggregation.sample(pageSize)), Video.class);
        return aggregate.getMappedResults();
    }

    @Override
    public Long hasFocus(Long userId, Long publishId) {
        return mongoTemplate.count(Query.query(Criteria.where("userId").is(userId).and("followUserId").is(publishId)), FollowUser.class);
    }

    @Override
    public List<Video> getVideosByUid(Long Uid) {
        return mongoTemplate.find(Query.query(Criteria.where("userId").is(Uid)), Video.class);
    }

    @Override
    public List<Video> pageVideoLogs(Integer page, Integer pageSize, String sortProp, String sub, Long uid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(uid));
        query.with(PageRequest.of(page - 1, pageSize));

        if ("desc".equals(sub)) {
            query.with(Sort.by(Sort.Order.desc("created")));
        } else if ("asc".equals(sub)){
            query.with(Sort.by(Sort.Order.asc("created")));
        }
        return mongoTemplate.find(query, Video.class);

    }

    @Override
    public Video getVideoById(ObjectId id) {
        return mongoTemplate.findOne(Query.query(Criteria.where("id").is(id)), Video.class);
    }

    @Override
    public void userFocus(FollowUser followUser) {
        mongoTemplate.save(followUser);
    }

    @Override
    public void userUnFocus(FollowUser followUser) {
        mongoTemplate.remove(Query.query(Criteria.where("userId").is(followUser.getUserId())
                        .and("followUserId").is(followUser.getFollowUserId()))
                , FollowUser.class);
    }

}
